package org.thrift;

import java.net.InetSocketAddress;

import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.thrift.service.QueryService;
import org.thrift.service.impl.QueryServiceImpl;

/**
 * @author donald 2018年3月12日 下午2:01:37
 */
public class ThriftServer {
	private static final Logger log = LoggerFactory.getLogger(ThriftServer.class);
	private final static String DEFAULT_HOST = "127.0.0.1";
	private final static int DEFAULT_PORT = 30001;
	private final static int DEFAULT_TIMEOUT = 5000;
	private static TServer server = null;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void main(String[] args) {
		try {
			InetSocketAddress address = new InetSocketAddress(DEFAULT_HOST,DEFAULT_PORT);
			TNonblockingServerSocket socket = new TNonblockingServerSocket(address,DEFAULT_TIMEOUT);
			QueryService.Processor processor = new QueryService.Processor(new QueryServiceImpl());
			TNonblockingServer.Args arg = new TNonblockingServer.Args(socket);
			arg.protocolFactory(new TBinaryProtocol.Factory());
			arg.transportFactory(new TFramedTransport.Factory());
			arg.processorFactory(new TProcessorFactory(processor));
			server = new TNonblockingServer(arg);
			log.info("start the Server...");
			server.serve();
			log.info("Server is running...");
			
		} catch (TTransportException e) {
			log.error("start Server error:", e);
		}
	}
}
